package com.hua.yuan.live.repertory.cloud_data.loader;
import android.support.annotation.NonNull;
import android.support.v4.app.LoaderManager;
import com.alibaba.fastjson.JSONObject;
import com.hua.yuan.common.https.OkHttpModule;
import com.hua.yuan.live.repertory.cloud_data.api.LiveAttentationApi;
import com.hua.yuan.live.repertory.cloud_data.res.LivePlayBean;
import com.hua.yuan.live.repertory.cloud_data.res.ResultData;

import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 关注主播
 */
public class LiveAttentationLoader {


    private LoaderManager.LoaderCallbacks<LivePlayBean> mCallback;

    private Disposable mPublish;

    public interface  onAttentationCallback{

        void attentation(Integer isSuccess);

    }


    /**
     * 获取主播的接口数据
     *
     * @param pToken    用户登录的token
     * @param pAnchorId 主播的Id
     */
    public static void attentation(@NonNull final String pToken, @NonNull final int pAnchorId,@NonNull final int pAttentation,
                                @NonNull final onAttentationCallback pCallback) {

        Observable.create(new ObservableOnSubscribe<Integer>() {

            @Override
            public void subscribe(ObservableEmitter<Integer> pEmitter) throws Exception {
                LiveAttentationApi mApi = OkHttpModule.getInstance().getRetrofit().create(LiveAttentationApi.class);
                Call<String> call = mApi.attention(pAnchorId,pAttentation,pToken);
                try {
                    Response<String> response = call.execute();
                    if (response.body() == null || response.body().equals("")) {
                        pEmitter.onNext(null);
                    }
                    ResultData resultData = JSONObject.parseObject(response.body(), ResultData.class);
                    if (resultData == null) {
                        pEmitter.onNext(null);
                        return;
                    }
                    pEmitter.onNext(resultData.getErrNo());
                    pEmitter.onComplete();
                } catch (IOException e) {
                    e.printStackTrace();
                    pEmitter.onError(e);
                }
            }

        }).subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<Integer>() {
            private Disposable mPublish;
            @Override
            public void onSubscribe(Disposable disposable) {
                mPublish=disposable;
            }

            @Override
            public void onNext(Integer isSuccess) {

                pCallback.attentation(isSuccess);
                mPublish.dispose();
            }

            @Override
            public void onError(Throwable throwable) {
                pCallback.attentation(-1);
                mPublish.dispose();
                throwable.printStackTrace();
            }

            @Override
            public void onComplete() {
                mPublish.dispose();
            }

        });
    }
}
